package com.yongcheon.sms_manager.ui.sms_spam.list;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.QuickContactBadge;
import android.widget.TextView;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.contract.ContactData;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.data.sms_spam.AbstractSMSData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.utils.DateUtil;
import com.yongcheon.sms_manager.utils.DialogUtil;
import com.yongcheon.sms_manager.vo.ContactVO;
import com.yongcheon.sms_manager.vo.SMSVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-11.
 */
public class SPAMCurosrAdapter extends CursorAdapter {
    LayoutInflater Inflater;
    AbstractSMSData smsData;
    ContactData contactData;

    public void setSMSService(AbstractSMSData smsData){
        this.smsData = smsData;
    }
    public void setContactData(ContactData contactData){
        this.contactData = contactData;
    }

    public SPAMCurosrAdapter(Context context, Cursor c) {
        super(context, c);
        Inflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View newView = Inflater.inflate(R.layout.spam_list, parent, false);
        return newView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        //contactData.refresh(view.getContext().getContentResolver()); // 주소록 신규 등록 시 처리
        SMSVO smsvo = smsData.createSMSVOFromCursor(cursor);
        ContactVO contactvo = contactData.getContactByPhoneNum(smsvo.getAddress());

        TextView represent = (TextView)view.findViewById(R.id.represent);
        represent.setText(smsvo.getAddress());

        TextView typeText = (TextView)view.findViewById(R.id.spam_type);
        typeText.setText(smsvo.getSpam_category());

        TextView title = (TextView)view.findViewById(R.id.title);
        title.setText(smsvo.getSpam_desc()+"( "+smsvo.getBody()+" )");

        TextView date = (TextView)view.findViewById(R.id.date);
        date.setText(DateUtil.formatTimeStampString(context, smsvo.getDate(), false));

        QuickContactBadge badge = (QuickContactBadge)view.findViewById(R.id.avatar);
        updateAvatarView(contactvo, smsvo, badge, context);

        setLonclickListener(view, smsvo, contactvo);
        setClickListener(view,smsvo, smsvo.getSpam_desc());
    }

    private void updateAvatarView(ContactVO contactvo, SMSVO smsvo, QuickContactBadge badge, Context context){
        Drawable avatarDrawable = context.getResources().getDrawable(R.drawable.ic_contact_picture);
        if(contactvo != null) {
            Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactvo.getContactid());
            badge.assignContactUri(contactUri);
            avatarDrawable = contactData.getAvatarDrawable(contactvo, context);
        }else{
            badge.assignContactFromPhone(smsvo.getAddress(), true);
            avatarDrawable = context.getResources().getDrawable(R.drawable.ic_contact_picture);
        }
        badge.setImageDrawable(avatarDrawable);
    }

    private SpamNumVO getSpamInfo(SMSVO smsvo, Context context){
        SpamNumVO spamNumVO = null;
        String subject = context.getResources().getString(R.string.unregist_desc);
        SpamNumberData spamNumberData = new SpamNumberData(context);
        ArrayList<SpamNumVO> spamNumVOList = spamNumberData.selectReturnVo(smsvo.getAddress(), context, SpamDBManager.getDatabase(context));
        if(spamNumVOList.size() > 0) spamNumVO = spamNumVOList.get(0);
        return spamNumVO;
    }

    private void setLonclickListener(View view, SMSVO smsvo, ContactVO contactvo){
        View.OnLongClickListener longClickListener = new SPAMLongClickListener(smsvo, contactvo, this);
        view.setOnLongClickListener(longClickListener);
    }

    private void setClickListener(final View view, final SMSVO smsvo, final String from){
        view.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                DialogUtil.showSMSMsgDlg(view.getContext(), from, DateUtil.formatTimeStampString(view.getContext(), smsvo.getDate(), false), smsvo.getBody());
            }
        });
    }
}
